package com.entor.springboot201912.controller;

import com.entor.springboot201912.model.Msg;
import com.entor.springboot201912.model.Student;
import com.entor.springboot201912.service.StudentServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sc")
public class StudentController extends BaseController{
    //按名称注入  表示一个接口可以有多个实现类，通过名字指定哪个实现类
    @Resource(name = "studentService")
    private StudentServiceI ssi;

    @RequestMapping("/find")
    @ResponseBody
    public List<Student> find(Student s){
        return ssi.find(s);
    }

    @RequestMapping("/index")
    public String index(){
        return "/student/index";
    }

    @RequestMapping("/aoe")
    public String aoe(HttpServletRequest req){
        String id = req.getParameter("id");
        if(id!=null){
            int tid = Integer.parseInt(id);//强制转为整型数
            Student s = new Student();
            s.setId(tid);
            s = ssi.get(s);
            //把值设置到跳转的页面
            req.setAttribute("stu",s);
        }
        return "/student/addOrEdit";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Msg add(Student s){
        return ssi.add(s);
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Msg edit(Student s){
        return ssi.edit(s);
    }


    @RequestMapping("/remove/{id}")
    @ResponseBody
    public  Msg remove( @PathVariable(value = "id") int id){
        return ssi.remove(id);
    }

    @ResponseBody
    @RequestMapping("/callPro/{cid}")
    public Map callPro(@PathVariable(value = "cid") int cid){
        return ssi.callProcedure(cid);
    }

    @ResponseBody
    @RequestMapping("/callRes/{cid}")
    public Map callRes(@PathVariable(value = "cid") int cid){
        return ssi.callResultSet(cid);
    }
}
